<?php
class ChannelCategory_model extends CI_Model {
    public $tabName;

    public function __construct()
    {
        $this->tabName = "kfsy_channel_store.kf_channel_category";
    }
    
    /*
        获取渠道分类列表
        params
            chn_id 渠道id
            store_id 店铺id
    */
    public function getCategoryList($chn_id = 0, $store_id = 0)
    {
        $where = array(
            "fk_channel_id" => $chn_id,
            "fk_store_id" => $store_id
        );

        $categoryList = $this->db
            ->order_by("id desc")
            ->get_where($this->tabName, $where)
            ->result_array();

        return $categoryList;
    }

    /*
        获取渠道分类名称
        params
            chn_id 渠道id
            store_id 店铺id
    */
    public function getCategoryName($chn_id = 0, $store_id = 0)
    {
        $where = array(
            "fk_channel_id" => $chn_id,
            "fk_store_id" => $store_id
        );

        $categoryList = $this->db
            ->select("other_category_id,other_category_name")
            ->order_by("id desc")
            ->get_where($this->tabName, $where)
            ->result_array();

        $res = array();

        foreach ($categoryList as $key => $value) {
            $res[$value['other_category_id']] = $value['other_category_name'];
        }

        return $res;
    }

    /*
        获取本地分类对应的第三方分类
        params
            chn_id 渠道id
            store_id 店铺id
    */
    public function getStoreCategoryToOtherCategory($chn_id = 0, $store_id = 0)
    {
        $where = array(
            "fk_channel_id" => $chn_id,
            "fk_store_id" => $store_id
        );

        $categoryList = $this->db
            ->select("fk_category_id,other_category_id,other_category_name")
            ->order_by("id desc")
            ->get_where($this->tabName, $where)
            ->result_array();

        $res = array();

        foreach ($categoryList as $key => $value) {
            $res[$value['fk_category_id']] = $value;
        }

        return $res;
    }

    /*
        添加渠道分类
        params
            chn_id 渠道id
            store_id 店铺id
            category_id 分类id
            category_name 分类名称
            category_rank 分类排序
    */
    public function addCategory($chn_id = 0, $store_id = 0, $category_id = 0, $category_name = '', $category_rank = 0)
    {
        $res = false;

        if($chn_id == 1){
            $res = $this->addMtCategory($store_id, $category_id, $category_name, $category_rank);
        }

        if($chn_id == 2){
            $res = $this->addBdCategory($store_id, $category_id, $category_name, $category_rank);
        }

        if($chn_id == 3){
            $res = $this->addJddjCategory($store_id, $category_id, $category_name);
        }

        if($chn_id == 4){
            $res = $this->addElemeCategory($store_id, $category_id, $category_name);
        }

        return $res;
        
    }

    /*
        添加百度分类
        params
            store_id 店铺id
            category_id 分类id
            category_name 分类名称
            category_rank 分类排序
    */
    public function addBdCategory($store_id = 0, $category_id = 0, $category_name = '', $category_rank = 0)
    {
        $this->load->model("BdApi_model", "BdApiModel");
        $BdApiModel = $this->BdApiModel;

        $add = $BdApiModel->addCategory($store_id, $category_id, $category_name, $category_rank);

        return $add;
    }

    /*
        添加美团分类
        params
            store_id 店铺id
            category_id 分类id
            category_name 分类名称
            category_rank 分类排序
    */
    public function addMtCategory($store_id = 0, $category_id = 0, $category_name = '', $category_rank = 0)
    {
        $this->load->model("MtApi_model", "MtApiModel");
        $MtApiModel = $this->MtApiModel;

        $add = $MtApiModel->addCategory($store_id, $category_id, $category_name, $category_rank);

        return $add;
    }

    /*
        添加饿了么分类
        params
            store_id 店铺id
            category_id 分类id
            category_name 分类名称
    */
    public function addElemeCategory($store_id = 0, $category_id = 0, $category_name = '')
    {
        $this->load->model("ElemeApi_model", "ElemeApiModel");
        $ElemeApiModel = $this->ElemeApiModel;

        $add = $ElemeApiModel->addCategory($store_id, $category_id, $category_name);

        return $add;
    }

    /*
        添加京东到家分类
        params
            store_id 店铺id
            category_id 分类id
            category_name 分类名称
    */
    public function addJddjCategory($store_id = 0,  $category_id = 0, $category_name = '')
    {
        $this->load->model("JddjApi_model", "JddjApiModel");
        $JddjApiModel = $this->JddjApiModel;

        $add = $JddjApiModel->addCategory($store_id, $category_id, $category_name);

        return $add;
    }

    /*
        删除渠道分类
        params
            chn_id 渠道id
            store_id 店铺id
            other_category_id 第三方分类id
    */
    public function delCategory($chn_id = 0, $store_id = 0, $other_category_id = '')
    {
        $res = false;

        if($chn_id == 1){
            $res = $this->delMtCategory($store_id, $other_category_id);
        }

        if($chn_id == 2){
            $res = $this->delBdCategory($store_id, $other_category_id);
        }

        if($chn_id == 3){
            $res = $this->delJddjCategory($store_id, $other_category_id);
        }

        if($chn_id == 4){
            $res = $this->delElemeCategory($store_id, $other_category_id);
        }

        return $res;
        
    }

    /*
        删除百度分类
        params
            store_id 店铺id
            other_category_id 第三方分类id
    */
    public function delBdCategory($store_id = 0, $other_category_id = '')
    {
        $this->load->model("BdApi_model", "BdApiModel");
        $BdApiModel = $this->BdApiModel;

        $del = $BdApiModel->delCategory($store_id, $other_category_id);

        return $del;
    }

    /*
        删除美团分类
        params
            store_id 店铺id
            other_category_id 第三方分类id
    */
    public function delMtCategory($store_id = 0, $other_category_id = '')
    {
        $this->load->model("MtApi_model", "MtApiModel");
        $MtApiModel = $this->MtApiModel;

        $del = $MtApiModel->delCategory($store_id, $other_category_id);

        return $del;
    }

    /*
        删除饿了么分类
        params
            store_id 店铺id
            other_category_id 第三方分类id
    */
    public function delElemeCategory($store_id = 0, $other_category_id = '')
    {
        $this->load->model("ElemeApi_model", "ElemeApiModel");
        $ElemeApiModel = $this->ElemeApiModel;

        $del = $ElemeApiModel->delCategory($store_id, $other_category_id);

        return $del;
    }

    /*
        删除京东到家分类
        params
            store_id 店铺id
            other_category_id 第三方分类id
    */
    public function delJddjCategory($store_id = 0, $other_category_id = '')
    {
        $this->load->model("JddjApi_model", "JddjApiModel");
        $JddjApiModel = $this->JddjApiModel;

        $del = $JddjApiModel->delCategory($store_id, $other_category_id);

        return $del;
    }

    /*
        更新渠道分类
        params
            chn_id 渠道id
            store_id 店铺id
            other_category_id 第三方分类id
            other_category_name 第三方分类名称
            other_category_rank 第三方分类排序
    */
    public function updateCategory($chn_id = 0, $store_id = 0, $other_category_id = 0, $other_category_name = '', $other_category_rank = 0)
    {
        $res = false;

        if($chn_id == 1){
            $res = $this->updateMtCategory($store_id, $other_category_id, $other_category_name, $other_category_rank);
        }

        if($chn_id == 2){
            $res = $this->updateBdCategory($store_id, $other_category_id, $other_category_name, $other_category_rank);
        }

        if($chn_id == 3){
            $res = $this->updateJddjCategory($other_category_id, $other_category_name);
        }

        if($chn_id == 4){
            $res = $this->updateElemeCategory($other_category_id, $other_category_name);
        }

        return $res;
        
    }

    /*
        更新百度分类
        params
            store_id 店铺id
            other_category_id 第三方分类id
            other_category_name 第三方分类名称
            other_category_rank 第三方分类排序
    */
    public function updateBdCategory($store_id = 0, $other_category_id = 0, $other_category_name = '', $other_category_rank = 0)
    {
        $this->load->model("BdApi_model", "BdApiModel");
        $BdApiModel = $this->BdApiModel;

        $del = $BdApiModel->updateCategory($store_id, $other_category_id, $other_category_name, $other_category_rank);

        return $del;
    }

    /*
        更新美团分类
        params
            store_id 店铺id
            other_category_id 第三方分类id
            other_category_name 第三方分类名称
            other_category_rank 第三方分类排序
    */
    public function updateMtCategory($store_id = 0, $other_category_id = 0, $other_category_name = '', $other_category_rank = 0)
    {
        $this->load->model("MtApi_model", "MtApiModel");
        $MtApiModel = $this->MtApiModel;

        $del = $MtApiModel->updateCategory($store_id, $other_category_id, $other_category_name, $other_category_rank);

        return $del;
    }

    /*
        更新饿了么分类
        params
            other_category_id 第三方分类id
            other_category_name 第三方分类名称
    */
    public function updateElemeCategory($other_category_id = 0, $other_category_name = '')
    {
        $this->load->model("ElemeApi_model", "ElemeApiModel");
        $ElemeApiModel = $this->ElemeApiModel;

        $del = $ElemeApiModel->updateCategory($other_category_id, $other_category_name);

        return $del;
    }

    /*
        更新京东到家分类
        params
            other_category_id 第三方分类id
            other_category_name 第三方分类名称
    */
    public function updateJddjCategory($other_category_id = 0, $other_category_name = '')
    {
        $this->load->model("JddjApi_model", "JddjApiModel");
        $JddjApiModel = $this->JddjApiModel;

        $del = $JddjApiModel->updateCategory($other_category_id, $other_category_name);

        return $del;
    }

    /*
        初始化渠道分类
        params
            chn_id 渠道id
            store_id 店铺id
    */
    public function initCategory($chn_id = 0, $store_id = 0)
    {
        $res = false;

        if($chn_id == 1){
            $res = $this->initMtCategory($store_id);
        }

        if($chn_id == 2){
            $res = $this->initBdCategory($store_id);
        }

        if($chn_id == 3){
            $res = $this->initJddjCategory($store_id);
        }

        if($chn_id == 4){
            $res = $this->initElemeCategory($store_id);
        }

        return $res;   
    }

    /*
        初始化美团分类
        params
            store_id 店铺id
    */
    public function initMtCategory($store_id = 0)
    {
        $this->load->model("MtApi_model", "MtApiModel");
        $MtApiModel = $this->MtApiModel;

        $init = $MtApiModel->initCategory($store_id);

        return $init;
    }

    /*
        初始化百度分类
        params
            store_id 店铺id
    */
    public function initBdCategory($store_id = 0)
    {
        $this->load->model("BdApi_model", "BdApiModel");
        $BdApiModel = $this->BdApiModel;

        $init = $BdApiModel->initCategory($store_id);

        return $init;
    } 

    /*
        初始化美团分类
        params
            store_id 店铺id
    */
    public function initJddjCategory($store_id = 0)
    {
        $this->load->model("JddjApi_model", "JddjApiModel");
        $JddjApiModel = $this->JddjApiModel;

        $init = $JddjApiModel->initCategory($store_id);

        return $init;
    } 

    /*
        初始化美团分类
        params
            store_id 店铺id
    */
    public function initElemeCategory($store_id = 0)
    {
        $this->load->model("ElemeApi_model", "ElemeApiModel");
        $ElemeApiModel = $this->ElemeApiModel;

        $init = $ElemeApiModel->initCategory($store_id);

        return $init;
    }  

}